﻿
#include "EGraphics.h"
#include "EWindow.h"
#include "EMeshUtil.h"
#include "EUtil.h"
#include "EGame.h"
#include "../res/resource.h"

using namespace Base3D;

//resource path to register
static void setup(){

}

#define BASE3D_REGISTER_PATH "Software\\Base3D\\"

//extern EString Base3D::DefaultPath;
Preference* Base3D::gPreference;

static int LoadSettings(){
    //char      path[1024] = "Software\\" REGKEY "\\" REGVERKEY "\\Windows\\Engine\\";
    HKEY      key;
    DWORD     type;
    DWORD     len;
    char buffer[1024] = {0};
    int retVal;

    gPreference = new Preference();

    if (RegOpenKeyEx(HKEY_CURRENT_USER, BASE3D_REGISTER_PATH, 0, KEY_READ, &key) == ERROR_SUCCESS) {
        if (RegQueryValueEx(key, "Path", nullptr, &type, (BYTE *) buffer, &len) == ERROR_SUCCESS || type == REG_SZ){
            retVal = len;
            //std::string b( buffer );
            //Preference::DefaultPath.assign( buffer );
            //gPreference->DefaultPath.assign( buffer );
            //gPreference->DefaultPath = buffer;
            strcpy_s( gPreference->buf, MAX_PATH, buffer );
        }else
            retVal = -1;
        RegCloseKey(key) ;
    }
    else
        retVal = -1;
    return retVal;
}

int WINAPI WinMain(
				   HINSTANCE hInstance,      // handle to current instance
				   HINSTANCE hPrevInstance,  // handle to previous instance
				   LPSTR lpCmdLine,          // command line
				   int nCmdShow              // show state
				   )
{	
	InitLog("Base3D.Log");
    //static
	Log("Init Graphics...");
	EGraphics::initGraphics(hInstance);

    LoadSettings();

	Log("Graphics Load Succeed!");

	//
	EGameWindow::GWindow = new EGameWindow("Base3D [3DTankWar]", hInstance);
	EGameWindow::GWindow->showWindow(true);

	// 设置游戏的小图标
	HWND hwnd = EGameWindow::GWindow->getHWnd();
	//
	//LONG iconID = (LONG)LoadIcon(::GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_ICON_TANKWAR));
	//::SetClassLong(hwnd, GCL_HICON, iconID);

    /*
     * */
	auto *game = new EGame;
	//onPaint
	EGameWindow::GWindow->setCanvasListener(game);
	//onKeyDown
	EGameWindow::GWindow->addInputListener(game);
	//
	EGameWindow::GWindow->startLoop();

	Log("Shutdown Graphics...");
	EGraphics::shutdownGraphics();

	CloseLog();

	return 0;
}